package mhys.Dao;

import mhys.Entity.Student;
import mhys.Entity.Teacher;
import mhys.Util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StuDao extends BaseDao{
    //查询全部学生
    public List<Student> getStudents(){
        String sql = "select s_id,s_name,s_number,sex,classes,phone from student";
        List<Student> students = new ArrayList<Student>();
        try {
            res = select(sql);
            while (res.next()){
                Integer s_id = res.getInt("s_id");
                String s_name = res.getString("s_name");
                String s_number = res.getString("s_number");
                String sex = res.getString("sex");
                String classes = res.getString("classes");
                String phone = res.getString("phone");
                Student student = new Student(s_id,s_name,s_number,sex,classes,phone);
                students.add(student);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(res,stmt,conn);
        }
        return students;
    }

    //按名字查询
    public List<Student> getStuByName(String name){
        String sql = "select s_id,s_name,s_number,sex,classes,phone from student where s_name like ?";
        List<Student> students = new ArrayList<Student>();
        try {
            res = select(sql,name);
            while (res.next()){
                Integer s_id = res.getInt("s_id");
                String s_name = res.getString("s_name");
                String s_number = res.getString("s_number");
                String sex = res.getString("sex");
                String classes = res.getString("classes");
                String phone = res.getString("phone");
                Student student = new Student(s_id,s_name,s_number,sex,classes,phone);
                students.add(student);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(res,stmt,conn);
        }
        return students;
    }

    //修改学生信息
    public int updateStu(Student student){
        String sql = "update student set s_number = ?,s_name = ?,sex = ?,classes = ?,phone =? where s_id = ?";
        return insertUpdateDelete(sql,student.getS_number(),student.getS_name(),student.getSex(),student.getClasses(),student.getPhone(),student.getS_id());
    }

    //删除学生
    public int delStu(Integer id){
        String sql = "delete from student where s_id = ?";
        return insertUpdateDelete(sql,id);
    }
    //添加学生
    public int addStu(Student student){
        String sql = "insert into student values(null,?,?,?,?,?)";
        return insertUpdateDelete(sql,student.getS_name(),student.getS_number(),student.getSex(),student.getClasses(),student.getPhone());
    }

    public static void main(String[] args) {
        StuDao stuDao = new StuDao();
        Student student = new Student(null,"江圣吉","123123123","男","信息创新22-7","654321");
        System.out.println(stuDao.updateStu(student));
    }
}
